package com.example.service.impl;

import com.example.domain.dto.DiseaseDTO;
import com.example.domain.po.Disease;
import com.example.domain.vo.DiseaseVo;
import com.example.mapper.DiseaseMapper;
import com.example.service.DiseaseService;
import com.example.util.PageBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class DiseaseServiceImpl implements DiseaseService {


    @Autowired
    private DiseaseMapper diseaseMapper;

    @Override
    public List<Disease> list() {


        return diseaseMapper.list();
    }

    @Override
    public List<Disease> listAll() {
        return diseaseMapper.listAll();
    }

    @Override
    public Integer addDisease(DiseaseDTO diseaseDTO) {
         diseaseMapper.addDisease(diseaseDTO);
        return diseaseDTO.getId();
    }

    @Override
    public void addItem(String s, String info, Integer id) {
        if (info.startsWith(",")){
            info = info.substring(1);
        }
        //diseaseMapper.addTiem(s,info,id);
        diseaseMapper.updateTiem(s,info,id);
    }

    @Override
    public PageBean<DiseaseVo> list(Integer pageNum, Integer pageSize, String name,String category) {
        PageBean<DiseaseVo> pb = new PageBean<>();
        PageHelper.startPage(pageNum, pageSize);
        List<DiseaseVo> list = diseaseMapper.listPage(name,category);


        list.forEach(DiseaseVo -> {

            if (DiseaseVo.getType()!=null && DiseaseVo.getType().equals(0) ) DiseaseVo.setTypeString("全部免费");
            if (DiseaseVo.getType()!=null && DiseaseVo.getType().equals(1)) DiseaseVo.setTypeString("会员免费");
            if (DiseaseVo.getType()!=null && DiseaseVo.getType().equals(2)) DiseaseVo.setTypeString("付费");
         if (DiseaseVo.getPic()!=null)   DiseaseVo.setPhotoList(DiseaseVo.getPic().split(","));
        });



        Page<DiseaseVo> p = (Page<DiseaseVo>)list ;
        pb.setItems(p.getResult());
        pb.setTotal(p.getTotal());
        return pb;
    }

    @Override
    public List<DiseaseVo> findById(Integer id) {


        var list = diseaseMapper.findById(id);

        list.forEach(DiseaseVo->{
            if (DiseaseVo.getType()!=null && DiseaseVo.getType().equals(0) ) DiseaseVo.setTypeString("全部免费");
            if (DiseaseVo.getType()!=null && DiseaseVo.getType().equals(1)) DiseaseVo.setTypeString("会员免费");
            if (DiseaseVo.getType()!=null && DiseaseVo.getType().equals(2)) DiseaseVo.setTypeString("付费");
            DiseaseVo.setPhotoList(DiseaseVo.getPic().split(","));

        });


     return list;
    }

    @Override
    public List<Map<String, Object>> listByName(String name) {
        return diseaseMapper.listByName(name);
    }

    @Override
    public List<DiseaseVo> getById(Integer id) {
        //增加浏览量
        diseaseMapper.addBrowse(id);
        List<DiseaseVo> byId = diseaseMapper.getById(id);

//        List<Keyword> keywords =  diseaseMapper.keyList();
//
//        for (DiseaseVo vo : byId) {
//            for (Keyword keyword : keywords) {
//                if (vo.getInfo().contains(keyword.getName())){
//                    vo.setInfo(vo.getInfo().replace(keyword.getName(), " <span class='clickable'>" + keyword.getName() + "</span>"));
//                }
//            }
//
//        }

        System.out.println(byId);
        return byId;
    }
    private static String replaceKeywordsWithLinks(String text, Map<String, String> keywordLinks) {
        for (Map.Entry<String, String> entry : keywordLinks.entrySet()) {
            String keyword = entry.getKey();
            String link = entry.getValue();
            // 使用正则表达式来确保匹配完整的单词
            text = text.replaceAll("\\b" + keyword + "\\b", "<a href=\"" + link + "\">" + keyword + "</a>");
        }
        return text;
    }
}
